package mrusanov.elo

class TeamEloRating(var rating:Int = 1000) extends Ordered[TeamEloRating]{

  def acceptDefeat(other:TeamEloRating) {
     acceptResult(0, other.rating)
  }

  def acceptVictory(other:TeamEloRating) {
     acceptResult(1, other.rating)
  }

  private def expectedResult(opponentRating:Int):Double = {
    val power = (opponentRating - this.rating) / 400d
    1 / (1d + scala.math.pow(10, power))
  }

  private def k = 24

  private def acceptResult(actualResult:Double, opponentRating:Int) {
    rating = (rating + k * (actualResult - expectedResult(opponentRating))).round.toInt
  }

  // if strength rating is two times bigger,
  // than chance to win for club is 67%
  def eloToStrength:Double = {
    // 200 difference means 0.76 vs 0.24 points difference
    scala.math.pow(0.76 / 0.24, rating / 200d).round.toInt
  }

  override def compare(that: TeamEloRating): Int = this.rating - that.rating
}
